package com.lh.web.controller.order;

import com.lh.common.entity.admin.Reason;
import com.lh.common.entity.admin.ReturnAddress;
import com.lh.common.enums.IsDel;
import com.lh.common.enums.ReasonEnum;
import com.lh.common.enums.UpperLowerShelfStatus;
import com.lh.common.utils.AssertUtil;
import com.lh.common.utils.Constant;
import com.lh.common.utils.PageUtils;
import com.lh.common.utils.Result;
import com.lh.web.controller.AbstractController;
import com.lh.web.service.serviceAdmin.order.ReasonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author FH
 * @ClassName: ReasonAction
 * @Description: (退款/售后原因)
 * @date: 2020-05-20 9:19
 */
@Api(value="ReasonAction",tags={"退款/售后原因"})
@RestController
@RequestMapping("reason")
public class ReasonAction extends AbstractController {

    @Autowired
    private ReasonService reasonService;

    /**
     * 退款/售后原因列表页面
     */
    @ApiOperation(value = "退款/售后原因列表页面")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "status", value = "状态", required = false, dataTypeClass= UpperLowerShelfStatus.class),
            @ApiImplicitParam(paramType="query", name = "type", value = "类型 REFUND:申请退款原因;AFTERSALES:申请售后原因;REFUSEREFUND:拒绝退款原因;WAREHOUS:拒绝入库原因", required = true, dataTypeClass= ReasonEnum.class)

    })
    @GetMapping("/getReasonList")
    @RequiresPermissions("reason:getReasonList")
    public Result<Reason> getReasonList(UpperLowerShelfStatus status,ReasonEnum type, Integer pageNum, Integer pageSize) {
        pageNum = pageNum== null ? 1:pageNum;
        pageSize = pageSize== null ? Integer.MAX_VALUE:pageSize;
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        if(status != null) {
            params.put("status", status.getValue());
        }
        if(type != null) {
            params.put("type", type.getValue());
        }
        PageUtils pageUtils = reasonService.queryReasonList(params, pageNum, pageSize);
        return Result.ok(pageUtils);
    }

    /**
     * 新增/修改退款/售后原因
     */
    @ApiOperation(value = "新增/修改退款/售后原因")
    @PostMapping("/setReason")
    @RequiresPermissions("reason:setReason")
    public Result<Reason> setReason(@RequestBody Reason reason) {
        try {
            AssertUtil.assertNotNull(reason.getContent(), Constant.RESULT.FI1000, "类容为空!");
            AssertUtil.assertNotNull(reason.getType(), Constant.RESULT.FI1000, "类型为空!");
            if(reason.getId() == null) {
                reasonService.insertReason(reason);
                return Result.ok(reason);
            }
            AssertUtil.assertNotNull(reason.getId(), Constant.RESULT.FI1000, "ID为空!");
            reasonService.updateReason(reason);
            return Result.ok(reason);
        } catch (Exception e) {
            return Result.error();
        }
    }

    @ApiOperation(value = "退款/售后上下架")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id", value = "id", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query",name = "status", value = "状态", required = false, dataTypeClass = UpperLowerShelfStatus.class),
    })
    @ResponseBody
    @GetMapping("/updateReasonStatus")
    @RequiresPermissions("reason:updateReasonStatus")
    public Result updateReasonStatus(Long id,UpperLowerShelfStatus status) {
        try {
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("id",id);
            if(status != null) {
                params.put("status",status.getValue());
            }
            reasonService.updateReasonStatus(params);
            return Result.ok();
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 退货地址列表页面
     */
    @ApiOperation(value = "退货地址列表页面")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "userName", value = "收货人", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "userPhone", value = "手机号", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "status", value = "状态", required = false, dataTypeClass= UpperLowerShelfStatus.class)

    })
    @GetMapping("/getReturnAddressList")
    @RequiresPermissions("reason:getReturnAddressList")
    public Result<ReturnAddress> getReturnAddressList(String userName, String userPhone,UpperLowerShelfStatus status, Integer pageNum, Integer pageSize) {
        pageNum = pageNum== null ? 1:pageNum;
        pageSize = pageSize== null ? Integer.MAX_VALUE:pageSize;
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        params.put("userName", userName);
        params.put("userPhone", userPhone);
        if(status != null) {
            params.put("status",status.getValue());
        }
        PageUtils pageUtils = reasonService.queryReturnAddressList(params, pageNum, pageSize);
        return Result.ok(pageUtils);
    }

    /**
     * 新增/修改退货地址
     */
    @ApiOperation(value = "新增/修改退货地址")
    @PostMapping("/setReturnAddress")
    @RequiresPermissions("reason:setReturnAddress")
    public Result<ReturnAddress> setReturnAddress(@RequestBody ReturnAddress returnAddress) {
        try {
            AssertUtil.assertNotNull(returnAddress.getAddress(), Constant.RESULT.FI1000, "地址为空!");
            AssertUtil.assertNotNull(returnAddress.getUserName(), Constant.RESULT.FI1000, "收货人为空!");
            AssertUtil.assertNotNull(returnAddress.getUserPhone(), Constant.RESULT.FI1000, "手机号为空!");
            if(returnAddress.getId() == null) {
                reasonService.insertReturnAddress(returnAddress);
                return Result.ok(returnAddress);
            }
            AssertUtil.assertNotNull(returnAddress.getId(), Constant.RESULT.FI1000, "ID为空!");
            reasonService.updateReturnAddress(returnAddress);
            return Result.ok(returnAddress);
        } catch (Exception e) {
            return Result.error();
        }
    }

    @ApiOperation(value = "退货地址上下架")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id", value = "id", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query",name = "status", value = "状态", required = false, dataTypeClass = UpperLowerShelfStatus.class)
    })
    @ResponseBody
    @GetMapping("/updateReturnAddressStatus")
    @RequiresPermissions("reason:updateReturnAddressStatus")
    public Result updateReturnAddressStatus(Long id,UpperLowerShelfStatus status) {
        try {
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("id",id);
            if(status != null) {
                params.put("status",status.getValue());
            }
            reasonService.updateReturnAddressStatus(params);
            return Result.ok();
        } catch (Exception e) {
            return Result.error();
        }
    }
}
